<?php
/**
 * Created by PhpStorm.
 * User: rocky
 * Date: 2019-07-14
 * Time: 11:18
 */

namespace app\api\controller\v1;


use app\api\service\WechatMiniService;
use app\common\controller\BaseApiController;
use app\common\facade\Token;
use app\common\service\Sms;
use app\shop\resource\UserResoure;
use think\facade\Log;
use think\facade\Validate;
use app\common\model\ApplyJoin;



class User extends BaseApiController
{
    //获取个人信息
    public function getInfo()
    {
        $store_id = $this->request->param('store_id');
        // $store_id = 0;
        $user = Token::user();
        $member = Token::member($store_id);
        $user['is_vipCard'] = $member[0];
        $user['expire_time'] = $member[1];
        $user['level'] = Token::level($store_id);
        $user['residue_num'] = Token::laundryNum($store_id)?Token::laundryNum($store_id):0;
        $this->successCode(UserResoure::item($user));
    }
    
    //修改个人信息
    public function putInfo()
    {
        $data = $this->request->except(['openid', 'unionid']);
        $data['id'] = Token::id();
        \app\common\model\User::update($data);
        $this->successCode();
    }

    //首次授权更新信息
    public function postInfo()
    {
        $iv = $this->request->param('iv');
        $iv2 = $this->request->param('iv2');
        $encryptedData = $this->request->param('encryptedData');
        $encryptedData2 = $this->request->param('encryptedData2');
        $res = WechatMiniService::updateInfo($iv, $iv2, $encryptedData, $encryptedData2);
        if ($res) {
            $mobile = Token::getVar('phone')?true:false;
            $name = Token::getVar('name')?true:false;
            $this->successCode(['mobile' => $mobile,'name'=>$name]);
        } else {
            $this->errorCode();
        }
    }

    //绑定微信手机号
    public function postWxPhone()
    {
        $iv = $this->request->param('iv');
        $encryptedData = $this->request->param('encryptedData');
        $phone = WechatMiniService::getPhone($iv, $encryptedData);
        if ($phone) {
            $user = Token::user();
            $user->phone = $phone;
            $user->save();
            $this->successCode();
        } else {
            $this->errorCode();
        }
    }

    /**
     * 发送验证码
     * @Author: rocky
     * 2019/8/17 17:01
     */
    public function postSms()
    {
        $phone = $this->request->post('phone');
        $type = $this->request->post('type', 1);
        $res = Validate::in($type, '1,2');
        $res ?: $this->errorCode(100, '类型错误');
        $desc = '';
        switch ($type) {
            case 1:
                $desc = '登录';
                break;
            case 2:
                $desc = '提现';
                break;
        }
        $res = Sms::sendRandNumber($phone, 6, $type, $desc);
        if ($res === true) {
            $this->successCode();
        } else {
            $this->errorCode(100, $res);
        }
    }
    
    /*
    申请入驻
    2022/10/20
    */
    public function postApplyjoin(){
        $uid = $this->request->param('uid');
        $username = $this->request->param('username');
        $name = $this->request->param('name');
        $phone = $this->request->param('phone');
        $store = $this->request->param('store');
        $address = $this->request->param('address');
        $imgList = $this->request->param('imgList');
        $Y_Img = $this->request->param('Y_Img');
        $idCart01 = $this->request->param('idCart01');
        $idCart02 = $this->request->param('idCart02');
        $data = [
            'uid'=>$uid, 
            'username'=>$username, 
            'name'=>$name, 
            'phone'=>$phone, 
            'store'=>$store, 
            'address'=>$address, 
            'imgList'=>$imgList,
            'Y_Img'=>$Y_Img,
            'idCart01'=>$idCart01,
            'idCart02'=>$idCart02
        ];
        $res = ApplyJoin::insert($data);
        $this->successCode($imgList);
        // halt($uid);
        
    }
}